using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Multiply
    /// </summary>
    public class Mul : Instruction
    {
        public override int CycleCount
        {
            get { return 1; }
        }

        public Mul()
            : base("mul")
        {
        }

        /// <summary>
        /// Multiplies the contents of registers rA and rB and puts the result 
        /// in register rD. This is a 32-bit by 32-bit multiplication that will 
        /// produce a 64-bit result. The least significant word of this value 
        /// is placed in rD. The most significant word is discarded.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            // (rD) <- LSW( (rA) x (rB) )

            uint ra = MicroblazeComponent.ReadRegister(SourceRegisterA);
            uint rb = MicroblazeComponent.ReadRegister(SourceRegisterB);

            ulong result = (ra * rb);

            MicroblazeComponent.WriteRegister(DestinationRegister, (uint)result, Address);
        }

        public override string ToString()
        {
            return String.Format("mul r{0}, r{1}, r{2}",
                DestinationRegister,
                SourceRegisterA,
                SourceRegisterB
            );
        }
    }
}
